/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Manager;

import Global.ConnectDB;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrator
 */
public class AccManager extends javax.swing.JPanel {
    ConnectDB connectdb;
    CallableStatement cst,cst2;
    ResultSet rs,rs2;
    CallableStatement cst_update;
    int flag = 0; //0: addnew, 1:edit
    DefaultTableModel model;
    /**
     * Creates new form AccManager
     */
    public AccManager() {
        initComponents();
        connectdb = new ConnectDB("1433","localhost","mssqlserver","eproject1","sa","123");
        connectdb.openConnection();
        cst_update = null;
        
        model =(DefaultTableModel) tbList.getModel();
        String name = "{call getAllName}";
        try{
            cst = connectdb.cn.prepareCall(name, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 
            rs = cst.executeQuery();
            
        }catch(SQLException ex){
            ex.printStackTrace();
        }
        editText(false);
        btSave.setEnabled(false);
        loadDataIntoTable();
    }
    public void loadDataIntoTable(){
        ResultSet rsload;
        model.setRowCount(0);
        try{
            String proc = "{call getAllName}";
             cst = connectdb.cn.prepareCall(proc, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        rsload = cst.executeQuery();
        while(rsload.next()){
                model.addRow(new String[]{rsload.getString(3), rsload.getString(4),rsload.getString(2)});
            }
        
        }catch(SQLException ex){
            ex.printStackTrace();
            }
    }
    public void callAllRole() {
        
        try {
            String type = "{call getAllRole}";
            cst2 = connectdb.cn.prepareCall(type, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs2 = cst2.executeQuery();
            rs2.moveToCurrentRow();
            while (rs2.next()) {
                cbb.addItem(rs2.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void editText(boolean ok){
        txfUser.setEditable(ok);
        txfPass.setEditable(ok);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txfUser = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txfPass = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cbb = new javax.swing.JComboBox();
        btSave = new javax.swing.JButton();
        btNew = new javax.swing.JButton();
        btEdit = new javax.swing.JButton();
        btDel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbList = new javax.swing.JTable();

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        jLabel1.setText("Account Manager");

        jLabel2.setText("Username:");

        txfUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfUserActionPerformed(evt);
            }
        });

        jLabel3.setText("Password:");

        jLabel4.setText("Role:");

        cbb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btSave.setText("Save");
        btSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSaveActionPerformed(evt);
            }
        });

        btNew.setText("New");
        btNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNewActionPerformed(evt);
            }
        });

        btEdit.setText("Edit");
        btEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditActionPerformed(evt);
            }
        });

        btDel.setText("Delete");
        btDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDelActionPerformed(evt);
            }
        });

        tbList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Username", "Password", "Role"
            }
        ));
        tbList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbList);
        tbList.getColumnModel().getColumn(1).setResizable(false);
        tbList.getColumnModel().getColumn(2).setResizable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 655, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addGap(53, 53, 53)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btSave)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txfUser)
                                        .addComponent(txfPass)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(cbb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(0, 0, Short.MAX_VALUE))))))
                        .addGap(111, 111, 111)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btDel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btEdit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btNew, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txfUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btNew))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btEdit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cbb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btDel))
                .addGap(18, 18, 18)
                .addComponent(btSave)
                .addGap(41, 41, 41)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(87, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    
    private void txfUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfUserActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txfUserActionPerformed

    private void btNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNewActionPerformed
        // TODO add your handling code here:
        editText(true);
        txfUser.setText("");
        txfPass.setText("");
        txfUser.grabFocus();
        btSave.setEnabled(true);
        cbb.removeAllItems();
        callAllRole();
        flag = 0;
    }//GEN-LAST:event_btNewActionPerformed

    private void btSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveActionPerformed
        // TODO add your handling code here:
        if(flag ==0){
        int confirm = JOptionPane.showConfirmDialog(this, "Are you want Save?", "Comfirm !!", JOptionPane.YES_NO_OPTION);
                if(confirm == JOptionPane.YES_OPTION){
        try{
            String getCbb = String.valueOf(cbb.getSelectedItem());
            rs.moveToInsertRow();
            rs.updateString(3, txfUser.getText().trim());
            rs.updateString(4, txfPass.getText().trim());
            rs.updateString(2, getCbb.trim());
            rs.insertRow();
            rs.moveToCurrentRow();
           
           loadDataIntoTable();
           btSave.setEnabled(false);
        }catch(SQLException ex){
            ex.printStackTrace();
        }
                }
        }
        else if(flag == 1){
                   int confirm = JOptionPane.showConfirmDialog(this, "Are you want Save?", "Comfirm !!", JOptionPane.YES_NO_OPTION);
                if(confirm == JOptionPane.YES_OPTION){
            try{
                String getCbb = String.valueOf(cbb.getSelectedItem());
                rs.updateString(3,txfUser.getText().trim());
                rs.updateString(4, txfPass.getText().trim());
                rs.updateString(2, getCbb.trim());
                rs.updateRow();                
                rs.moveToCurrentRow();
                
                loadDataIntoTable();
                editText(false);
                btSave.setEnabled(false);
            }catch(SQLException ex){
                ex.printStackTrace();
            }
                                        } 
                }
    }//GEN-LAST:event_btSaveActionPerformed

    private void btEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditActionPerformed
        // TODO add your handling code here:
        editText(true);
        btSave.setEnabled(true);
        flag = 1;
        txfUser.grabFocus();
        cbb.removeAllItems();
        callAllRole();
    }//GEN-LAST:event_btEditActionPerformed

    private void btDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDelActionPerformed
        // TODO add your handling code here:
        int confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Comfirm !!", JOptionPane.YES_NO_OPTION);
                if(confirm == JOptionPane.YES_OPTION){
            try{
                rs.deleteRow();    
                    rs = cst.executeQuery();
                    loadDataIntoTable();
                    if(rs.next()){
                        txfUser.setText(String.valueOf(rs.getInt(1)));
                        txfPass.setText(rs.getString(2));
                    }
                    
                }catch(SQLException ex){
                    ex.printStackTrace();
                }
            
                }
    }//GEN-LAST:event_btDelActionPerformed

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_formMouseClicked

    private void tbListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbListMouseClicked
        // TODO add your handling code here:
        try {
            int row = tbList.rowAtPoint(evt.getPoint());
            if (rs.absolute(row + 1)) {
                cbb.removeAllItems();
                txfUser.setText(rs.getString(3));
                txfPass.setText(rs.getString(4));
                cbb.addItem(rs.getString(2));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_tbListMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btDel;
    private javax.swing.JButton btEdit;
    private javax.swing.JButton btNew;
    private javax.swing.JButton btSave;
    private javax.swing.JComboBox cbb;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbList;
    private javax.swing.JTextField txfPass;
    private javax.swing.JTextField txfUser;
    // End of variables declaration//GEN-END:variables
}
